﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO;
using System.Media;
using WMPLib;

namespace MyTest
{
    /// <summary>
    /// 教员:薛海
    /// 微信:xuehai03
    /// QQ:289477003
    /// 简易播放器 支持加减音量
    /// 2019-2-20
    /// </summary>
    public partial class FrmMusic : Form
    {
        public FrmMusic()
        {
            InitializeComponent();
        }

        public int index = 1;
        public int listIndex;
        SoundPlayer player = new SoundPlayer();
        Dictionary<string, Music> dic = new Dictionary<string, Music>();
        IWMPMedia media;

        List<Music> list = new List<Music>();

        //导入文件事件
        private void tsmiImport_Click(object sender, EventArgs e)
        {
            Music music = new Music();

            FileInfo fi = null;
            ListViewItem lvi = null;
            openFileDialog1.Multiselect = true;
            openFileDialog1.Filter = "mp3文件(*.mp3)|*.mp3|wav文件(*.wav)|*.wav|wma文件(*.wma)|*.wma|mp4文件(*.mp4)|*.mp4";

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                foreach (string filePath in openFileDialog1.FileNames)
                {
                    fi = new FileInfo(filePath);

                   // music.Path = filePath.Remove(filePath.LastIndexOf('.'))+".mp3"; //歌曲类型
                    music.Path = filePath; //歌曲路径

                    music.FileType = filePath.Remove(0, filePath.LastIndexOf('.')); //歌曲类型

                    music.FileName = music.Path.Remove(0, music.Path.LastIndexOf('\\') + 1); //歌曲名称

                    music.FileSize = (fi.Length / 1024).ToString() + "KB";  //歌曲大小
                    

                    music.FileNo = index++.ToString(); //歌曲编号

                    list.Add(music);


                    lvi = new ListViewItem(music.FileNo);

                    lvi.SubItems.AddRange(new string[] { music.FileName, music.FileType, music.FileSize, music.Path });

                    if (this.listView1.Items.Count > 0)
                    {
                        if (!dic.ContainsKey(filePath))
                        {
                            listView1.Items.Add(lvi);

                            dic.Add(filePath, music);
                            //添加到播放列表
                            media = this.axWindowsMediaPlayer1.newMedia(filePath);
                            axWindowsMediaPlayer1.currentPlaylist.insertItem(listIndex++, media);
                        }
                        else
                        {
                            index--;
                            MessageBox.Show("文件已存在！");
                        }
                    }
                    else
                    {
                        this.listView1.Items.Add(lvi);
                        dic.Add(filePath, music);
                        //添加到播放列表
                        media = this.axWindowsMediaPlayer1.newMedia(filePath);
                        this.axWindowsMediaPlayer1.currentPlaylist.insertItem(listIndex++, media);
                    }


                }
            }

            #region 
            ////文件名  类型  大小  路径
            //string fileName, fileExtension, fileSize, temp;
            ////文件对象
            //FileInfo fi = null;

            //ListViewItem lvi = null;

            //openFileDialog1.Multiselect = true;
            ////获得文件类型
            //openFileDialog1.Filter = "mp3文件(*.mp3)|*.mp3|wav文件(*.wav)|*.wav|wma文件(*.wma)|*.wma";

            //if (openFileDialog1.ShowDialog() == DialogResult.OK)
            //{
            //    foreach (string filePath in openFileDialog1.FileNames)
            //    {
            //        fi = new FileInfo(filePath);
            //        temp = filePath.Remove(filePath.LastIndexOf('.')); //歌曲类型
            //        fileName = temp.Remove(0, temp.LastIndexOf('\\') + 1); //歌曲名称
            //        fileExtension = filePath.Remove(0, filePath.LastIndexOf('.')); //歌曲路径
            //        fileSize = (fi.Length / 1024).ToString() + "KB";  //歌曲大小

            //        lvi = new ListViewItem();
            //        //歌曲编号
            //        lvi.Text = index++.ToString();
            //        lvi.SubItems.AddRange(new string[] { fileName, fileExtension, fileSize, filePath });

            //        if (this.listView1.Items.Count > 0)
            //        {
            //            if (!dic.ContainsKey(filePath))
            //            {
            //                listView1.Items.Add(lvi);
            //                dic.Add(filePath, fileName);
            //                //添加到播放列表
            //                media = this.axWindowsMediaPlayer1.newMedia(filePath);
            //                axWindowsMediaPlayer1.currentPlaylist.insertItem(listIndex++, media);
            //            }
            //            else
            //            {
            //                index--;
            //                MessageBox.Show("文件已存在！");
            //            }
            //        }
            //        else
            //        {
            //            this.listView1.Items.Add(lvi);
            //            dic.Add(filePath, fileName);
            //            //添加到播放列表
            //            media = this.axWindowsMediaPlayer1.newMedia(filePath);
            //            this.axWindowsMediaPlayer1.currentPlaylist.insertItem(listIndex++, media);
            //        }
            //    }
            //}

            #endregion
        }

        //双击播放
        private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {

            if (this.listView1.SelectedItems.Count > 0)
            {
                int index = int.Parse(this.listView1.SelectedItems[0].Text) - 1;
                var list = this.axWindowsMediaPlayer1.playlistCollection.getAll();
                Console.WriteLine(list.count);
                this.axWindowsMediaPlayer1.URL = this.listView1.SelectedItems[0].SubItems[4].Text;
            }
        }

        //播放
        private void btnPlay_Click(object sender, EventArgs e)
        {

            if (this.btnPlay.Text.Equals("播放"))
            {
                this.axWindowsMediaPlayer1.Ctlcontrols.play();
                this.btnPlay.Text = "暂停";
            }
            else
            {
                this.axWindowsMediaPlayer1.Ctlcontrols.pause();
                this.btnPlay.Text = "播放";
            }
        }

        //停止事件
        private void btnStop_Click(object sender, EventArgs e)
        {
            this.axWindowsMediaPlayer1.Ctlcontrols.stop();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {

        }

        //加音量
        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.axWindowsMediaPlayer1.settings.volume += 10;
        }

        //减音量
        private void btnJin_Click(object sender, EventArgs e)
        {
            this.axWindowsMediaPlayer1.settings.volume -= 10;
        }

       
    }
}
